﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using BibMan.controller;
using System.Windows.Forms;

namespace BibMan.gui {

    public delegate void searchHandler();

    /// <summary>
    /// A search box for use on a toolstrip
    /// </summary>
    /// <remarks>
    /// THIS IS NOT USED IN THE CURRENT VERSION OF BIBMAN BECAUSE OF VS DESIGNER ISSUES.
    /// IT WILL BE USED IN FUTURE, WHEN DESIGNER IS PHASED OUT OF THE PROJECT
    /// </remarks>
    public partial class ToolStripSearchBox : ToolStripTextBox {

        public event searchHandler SearchPerformed;
        private void FireSearchPerformed() {
            if (this.SearchPerformed != null) SearchPerformed();
        }

        public ToolStripSearchBox() {
            InitializeComponent();
        }

        public ToolStripSearchBox(IContainer container)
            : this() {
                container.Add(this);
        }

        public IEnumerable<String> SearchResult { get; private set; }

        private void OnTextChanged(object sender, EventArgs e) {
            DoSearch();
        }

        private void DoSearch() {
            Searcher searcher = new Searcher(this.Text);
            this.SearchResult = searcher.Search();
            FireSearchPerformed();
        }

        
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
            if (keyData == (Keys.Control | Keys.Back)) {
                SendKeys.SendWait("^+{LEFT}{BACKSPACE}");
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        
    }
}
